using System;
using System.Collections.Generic;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.DAO;
using GymManager.Persistence.VO;
using GymManager.Properties;

namespace GymManager.Business
{
    /// <summary>
    /// Class responsible by providing services regarding reminders
    /// </summary>
    public class ReminderServices
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ReminderServices));

        /// <summary>
        /// Method responsible by getting information about all avaliable reminders,
        /// in according to the user preferences
        /// </summary>
        /// <returns>List of all avaliable reminders</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<Reminder> findAll()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }
            
            List<Reminder> reminderList = new List<Reminder>();

            try
            {
                // getting information about reminders
                ReminderDAO reminderDAO = new ReminderDAO();

                // getting birthday reminders
                if (Settings.Default.ShowBirthdayReminders)
                {                    
                    reminderList.AddRange(reminderDAO.findBirthdayReminders());
                }

                // getting medical exam reminder
                if (Settings.Default.ShowMedicalExamReminders)
                {
                    reminderList.AddRange(reminderDAO.findMedicalExamReminders());
                }

                // getting monthly fee debtors
                if (Settings.Default.ShowMonthlyFeeDebtors)
                {
                    reminderList.AddRange(reminderDAO.findMonthlyFeeDebtorsReminders());
                }

                // getting stock item debtors reminder
                if (Settings.Default.ShowStockItemsDebtors)
                {
                    reminderList.AddRange(reminderDAO.findStockItemDebtorsReminders(Settings.Default.StockItemsDebtorsDaysTolerance));
                }

                // getting stock item low position reminder
                if (Settings.Default.ShowLowStockPositionItems)
                {
                    reminderList.AddRange(reminderDAO.findLowStockPositionItemsReminders( Settings.Default.StockItemMinimumItemsQuantity ));
                }
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }            
            
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return reminderList;
        }
    }
}
